module RowlingsWizard
  class Spellbook
    attr_reader :spells

    def initialize
      @spells = spells_data
    end

    def spell(spell_name)
      if spells[spell_name].nil?
        "Spell \"#{spell_name}\" not found in spellbook."
      else
        build_spell(spell_name)
      end
    end

    def random_spell
      spell_name = spells.keys.shuffle.first
      build_spell(spell_name)
    end

    private

    def spells_data
      HashWithIndifferentAccess.new(
        YAML.load(
          File.read(
            File.expand_path('../spells.yml', __FILE__)
          )
        )
      )
    end

    def build_spell(spell_name)
      spell = spells[spell_name]
      spell[:name] = spell_name
      spell
    end
  end
end